package com.fancy.mobileschool.service;


import com.fancy.mobileschool.entity.GroupChatMessage;
import com.fancy.mobileschool.util.GsonUtil;
import com.fancy.mobileschool.util.OKHttpUtil;
import com.google.gson.Gson;
import java.util.List;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Jackie on 2017/11/4.
 *
 */

public class ChatGroupService {

    private OkHttpClient httpClient = OKHttpUtil.getHttpClient();

    private Gson gson = GsonUtil.getGson();

    private static final String baseUrl =  "http://120.78.197.155:8080/MobileSchool-chat/group_chat";

    private static final String yun2 = "http://101.200.35.107:8080/MobileSchool-chat/group_chat";

    private static final String baseUrl1 = "http://192.168.1.101:8081/group_chat";

    public List<GroupChatMessage> getGroupConversation(String group_id) {
        List<GroupChatMessage> rs;
        String url = baseUrl + "/getGroupConversation.do?group_id=" + group_id;
        rs = OKHttpBase.fromJsonListByGet(url, GroupChatMessage.class);
        return rs;
    }



    public boolean sendGroupMessage(GroupChatMessage message) {
        boolean result = false;
        try {
            FormBody body = new FormBody.Builder()
                    .add("body", gson.toJson(message)).build();
            Request request = new Request.Builder().url(baseUrl + "/sendMessage.do").post(body).build();
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200){
                result = true;
            }
        }catch (Exception e) {
            result = false;
        }

        return result;
    }

}
